<?PHP
session_start();
include("../config/connect.php");
include("../public/common.php");

if( !isset($_SESSION["gs_user"]) ) 
{
	$url = "Location:login.php";
	header($url);
}

$showtype = CheckUrlParamNoDie("showtype");
$id = SetUrlParamInt("id");
$opt = CheckUrlParamNoDie( "opt" );

$nPage = SetUrlParamInt("page");
if( $nPage < 0 )
	$nPage = 0;
	
$input_item = CheckUrlParamNoDie( "input_item" );
if( $input_item == "" )
{
	if( $showtype == "a" )
		$sql = "select * from trade where 1 order by id desc";
	else if( $showtype == "p" )
	{
		$sql = "select * from trade where has_return=2 order by id desc";
	}
	else 
		$sql = "select * from trade where has_return=0 order by id desc";
}
else
{
	$sql = "select * from trade where item_id='$input_item' order by id desc";
}

$result = mysql_query( $sql );
$row_num = mysql_num_rows( $result );

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>购物兔</title>
<style type="text/css">
<!--
body,td,th {
	font-family: 宋体;
	font-size: 13px;
	color: #000;
}
body {
	background-color: #FFF;
}
-->
</style>
<script language="javascript">

function ShowAll(  )
{
	window.location.href= "view_discount.php?showtype=a";
	//window.location.reload();
}

function ShowDefault(  )
{
	window.location.href= "view_discount.php";
	//window.location.reload();
}

function ShowPart(  )
{
	window.location.href= "view_discount.php?showtype=p";
	//window.location.reload();
}

function ShowAllPart(  )
{
	window.location.href= "view_discount.php?showtype=ap";
	//window.location.reload();
}

function OnFind()
{
	var input_item = document.getElementById("input_item").value;
	window.location.href= "view_discount.php?showtype=<?PHP echo $showtype; ?>&input_item=" + input_item ;
}

function OnPageChange( type )
{
	if( type == "pre" )
	{
		window.location.href= "view_discount.php?showtype=<?PHP echo $showtype; ?>&input_item=<?PHP echo $input_item; ?>" + "&page=<?PHP echo $nPage - 1; ?>";
	}
	else if( type == "next" )
	{
		window.location.href= "view_discount.php?showtype=<?PHP echo $showtype; ?>&input_item=<?PHP echo $input_item; ?>" + "&page=<?PHP echo $nPage + 1; ?>";
	}
	else if( type == "first" )
	{
		window.location.href= "view_discount.php?showtype=<?PHP echo $showtype; ?>&input_item=<?PHP echo $input_item; ?>" + "&page=0";
	}
	else if( type == "last" )
	{
		window.location.href= "view_discount.php?showtype=<?PHP echo $showtype; ?>&input_item=<?PHP echo $input_item; ?>" + "&page=<?PHP echo floor($row_num/20); ?>";
	}
	
}

//var timeoutID = setTimeout("Update( )", 60000);
</script>
</head>

<body>
<!--<a href="add_trade.php?opt=add">添加记录</a>-->
<table cellpadding="0" cellspacing="0" border="1" align="center">
	<tr>
    	<td height="25" width="50" align="center">序列号</td>
        <td width="200" align="center">时间</td>
      	<td width="198" align="center">产品Id</td>
        <td width="70" 	align="center">返现(元)</td>
        <td width="144" align="center">交易流水号</td>
        <td width="158" align="center">支付宝帐号</td>
        <td width="113" align="center">ip</td>
        <td width="60" align="center">转换</td>
        <td width="143" align="center">操作</td>
    </tr>
<?php

//全部返现
if( $id != 0 && $opt == "all" )
{
	$sql = "update trade set has_return=1 where id=$id limit 1";
	mysql_query( $sql );
	//header("Location:view_discount.php?showtype=".$showtype);
}

//部分返现
if( $id != 0 && $opt == "part" )
{
	$sql = "update trade set has_return=2 where id=$id limit 1";
	mysql_query( $sql );
	//header("Location:view_discount.php?showtype=".$showtype);
}

$limit = " limit ". $nPage*20 .",20";

if( $input_item == "" )
{
	$search = " ";
}
else
{
	$search = " and item_id='$input_item' ";
}

if( $showtype == "a" )
{
	$sql = "select * from trade where 1 ".$search." order by id desc".$limit;
}
else if( $showtype == "p" )
{
	$sql = "select * from trade where has_return=2".$search."order by id desc".$limit;
}
else if( $showtype == "ap" )
{
	$sql = "select * from trade where has_return=1".$search."order by id desc".$limit;
}
else
{
	$sql = "select * from trade where has_return=0".$search."order by id desc".$limit;
}
	
$result = mysql_query( $sql );
while( $result && $row = mysql_fetch_assoc( $result ))
{
	if( $row["use_discount"] == "1" )
		$use_discount = "<strong><span style=\"color:#F00\">是</span></strong>";
	else
		$use_discount = "否";
?>
<tr title="<?PHP echo $row["info"]; ?>">
    <td height="25" width="50" align="center"><?PHP echo $row["id"]; ?></td>
    <td width="200" align="center" title=""><?PHP echo $row["time"]; ?></td>
	<td width="198" align="center"><?PHP echo $row["item_id"]; ?>&nbsp;</td>
	<td width="70" 	align="center"><?PHP echo $row["discount"]; ?>&nbsp;</td>
	<td width="144" align="center"><a href="view_trade.php?page=<?PHP echo $nPage; ?>&trade=<?PHP echo $row["trade_id"]; ?>&showtype=<?PHP echo $showtype; ?>"><?PHP echo $row["trade_id"]; ?></a>&nbsp;</td>
	<td width="158" align="center"><a href="view_user.php?all=1&tb_id=<?PHP echo $row["tb_id"]; ?>"><?PHP echo $row["tb_id"]; ?></a>&nbsp;</td>
    <td width="113" align="center"><?PHP echo $row["ip"]; ?>&nbsp;</td>
    <td width="60" align="center"><?PHP echo $use_discount; ?>&nbsp;</td>
	<td width="143" align="center"><a href="view_discount.php?page=<?PHP echo $nPage; ?>&opt=all&id=<?PHP echo $row["id"]; ?>&showtype=<?PHP echo $showtype; ?>">全部返现</a>
    <a href="view_discount.php?page=<?PHP echo $nPage; ?>&opt=part&id=<?PHP echo $row["id"]; ?>&showtype=<?PHP echo $showtype; ?>">部分返现</a></td>
</tr>
<?PHP
}
?>
</table>
<table cellpadding="0" cellspacing="0" border="0" align="center">
	<tr>
    	<td width="800" height="30" align="center">产品ID:<input type="text" value="<?PHP echo $input_item; ?>" id="input_item" size="20"/><input type="button" onclick="javascript:OnFind();" value="查找" />&nbsp;<input type="button" value="显示所有" onclick="javascript:ShowAll()" />&nbsp;
    	  <input type="button" value="未返现" onclick="javascript:ShowDefault()" />&nbsp;&nbsp;<input type="button" value="部分返现" onclick="javascript:ShowPart()" />&nbsp;<input type="button" value="全部返现" onclick="javascript:ShowAllPart()" />&nbsp;<a href="#" onclick="javascript:OnPageChange('first');">首页</a>&nbsp;&nbsp;<a href="#" onclick="javascript:OnPageChange('next');">下页</a>&nbsp;&nbsp;<a href="#" onclick="javascript:OnPageChange('pre');">上页</a>&nbsp;&nbsp;<a href="#" onclick="javascript:OnPageChange('last');">末页</a>;</td>
    </tr>
</table>
</body>
</html>
